# This Python file uses the following encoding: utf-8
import models
import logging

def AddSOVote(mid,uid,vstring,sentiment):
    vsl = len(vstring.split(" "))
    if vstring.startswith("@") and vsl == 1:
        type = "user"
    elif vstring.startswith("#") and vsl == 1:
        type = "hashtag"
    else:
        type = "text"
    sob=models.SentObject.all()
    so=sob.filter('vstring =',vstring).fetch(1)
    if not so:
        so=models.SentObject(type=type,vstring=vstring)
        so.put()
    else:
        so=so[0]

    ov=models.SentObjectVote.all()
    ov.filter('uid =',uid)
    ov.filter('so =',so)
    ov=ov.fetch(1)
    if not ov:
        ov= models.SentObjectVote(uid=uid,so=so)
        ov.dirty = 1
        ov.mid = mid
        ov.psentiment = sentiment
        ov.sentiment = sentiment
        ov.put()
    else:
        ov=ov[0]
        ov.mid = mid
        ov.sentiment = sentiment
        if ov.psentiment != sentiment:
            if ov.dirty == 0:
                ov.dirty = 2
        else:
            if ov.dirty == 2:
                ov.dirty = 0
        ov.put()
            
def GetLastVoteMsgId():
    v=models.SentObjectVote.all().order("-mid")
    f = v.fetch(1)
    if not f:
        return f
    return f[0].mid

def VoteCalcs(pos,neg):
    total = pos+neg
    posrate = float(pos)/float(total)
    negrate = float(neg)/float(total)
    rate = (posrate - negrate) * 100
    return total, rate

def ProcessSOVotes(date):
    pd = {}
    ov = models.SentObjectVote.all()
    ov.filter("dirty >", 0)
    ov=ov.fetch(100000)
    logging.info("Processing %d dirty votes" %(len(ov)))
    for o in ov:
        ol=models.SentObjectLog.all().order("-date")
        ol.filter('so =',o.so)
        ols=ol.fetch(2)
        if len(ols) > 0 and ols[0].date == date:
            ol = ols[0]
            if len(ols) == 2:
                pl = ols[1]
            else:
                pl = None
        else:
            ol= models.SentObjectLog(date=date,so=o.so)
            if len(ols) > 0:
                pl = ols[0]
                ol.pos = pl.pos
                ol.neg = pl.neg
            else:
                ol.pos = 0
                ol.neg = 0
                pl = None
        if o.dirty == 1:
            if o.sentiment: 
                ol.pos+=1
            else:
                ol.neg+=1
        else:
            if o.sentiment: 
                ol.pos+=1
                ol.neg-=1
            else:
                ol.pos-=1
                ol.neg+=1
        total,rate = VoteCalcs(ol.pos,ol.neg)
        ol.total=total
        ol.rate=rate
        if pl:
            ol.ratediff = ol.rate - pl.rate
        else:
            ol.ratediff = 0.0
        ol.put()
        o.dirty = 0
        o.psentiment = o.sentiment
        o.put()
    logging.info("%d dirty votes processed" %(len(ov)))

def GetWinners():
    winners = models.SentObjectLog.all()
    winners.filter("ratediff >", 0)
    q=winners.order('-ratediff')
    winners = q.order('-total').fetch(6)
    logging.info(winners)
    return winners
    
def GetLoosers():
    loosers = models.SentObjectLog.all()
    loosers.filter("ratediff <", 0)
    q=loosers.order('ratediff')
    loosers = q.order('-total').fetch(6)
    return loosers
